package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.TypeListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CertificateTypeContent;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.utils.LogUtil;
import java.util.ArrayList;
import java.util.HashMap;

public class TypeListFragment extends BaseListFragment {

  private ArrayList<CertificateTypeContent> mList;
  private TypeListAdapter mAdapter;
  private BaseVolleyActivity mACctivity;
  private CurrentUser mUser;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    mACctivity = (BaseVolleyActivity) getActivity();
    mUser = CurrentUser.newInstance(getActivity());
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.employeeLoanType);
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    super.onCreate(savedInstanceState);
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    // 执行完父类方法才存在mListView.
    super.onViewCreated(view, savedInstanceState);
    mListview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    mListview.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mAdapter.notifyDataSetChanged();
      }
    });
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

      case R.id.menu_item_confirm:
        if (mAdapter != null) {
          //选择后要清空之前的数据.用新建对象的方式清空.
          ArrayList<String> codes = new ArrayList<String>();
          ArrayList<String> names = new ArrayList<String>();
          //				for (int i = mAdapter.getCount() - 1; i >= 0; i--) {
          for (int i = 0; i < mAdapter.getCount(); i++) {
            LogUtil.i("mAdapterisChecked=" + i + mAdapter.isCheckedItem(i));
            if (mAdapter.isCheckedItem(i) == true) {
              LogUtil.i("选中的类型名==" + mAdapter.getItem(i).getCertificateName());
              codes.add(mAdapter.getItem(i).getCertificateCode());
              names.add(mAdapter.getItem(i).getCertificateName());
            }
          }
          StringBuilder codeBuilder = new StringBuilder();
          StringBuilder nameBuilder = new StringBuilder();

          for (int i = codes.size() - 1; i >= 0; i--) {
            if (codeBuilder.length() > 0) {
              codeBuilder.append(",").append(codes.get(i));
            } else {
              codeBuilder.append(codes.get(i));
            }
            if (nameBuilder.length() > 0) {
              nameBuilder.append(",").append(names.get(i));
            } else {
              nameBuilder.append(names.get(i));
            }
          }

          Intent i = new Intent();
          i.putExtra(SearchDepatrtCollectFragment.EXTRA_CERTIFICATE_CODES, codeBuilder.toString());
          i.putExtra(SearchDepatrtCollectFragment.EXTRA_CERTIFICATE_NAMES, nameBuilder.toString());
          getActivity().setResult(Activity.RESULT_OK, i);
          getActivity().finish();

          LogUtil.i("codeBuilder===" + codeBuilder);
          LogUtil.i("nameBuilder===" + nameBuilder);
        }
        break;

      default:
        break;
    }

    return super.onOptionsItemSelected(item);
  }

  @Override public String getUrl() {

    return ApiConst.ULR_TYPE_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("memberCode", mUser.getMemberCode());
    params.put("isPaging", "0");
    params.put("style", "tree");
    params.put("nodeKind", "02");
    params.put("certificateType", "oa");
    params.put("orderField", "LISTKIND_CODE,order_no");
    params.put("orderSeq", "asc");
    return params;
  }

  @Override public void resolveResponse(String json) {
    CertificateTypeContent[] contents = new Gson().fromJson(json, CertificateTypeContent[].class);
    //将数组转化为集合
    ArrayList<CertificateTypeContent> rows = new ArrayList<CertificateTypeContent>();
    for (int i = 0; i < contents.length; i++) {
      rows.add(contents[i]);
    }
    if (mList == null) {
      mList = new ArrayList<CertificateTypeContent>();
    }
    mList.clear();
    mList.addAll(rows);
    setAdapter(mList);
  }

  private void setAdapter(ArrayList<CertificateTypeContent> mList2) {
    if (mAdapter == null) {
      mAdapter = new TypeListAdapter(getActivity(), mList2);
    }
    setListAdapter(mAdapter);
    ((TypeListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
  }
}
